<template>
  <div class="app-container">
    <el-button type="success" icon="el-icon-finished" style="margin: 10px;" @click="handleSubmit">
      提交
    </el-button>
    <el-button type="info" icon="el-icon-refresh" style="margin: 10px;" @click="handleRefresh">
      重置
    </el-button>
    <el-button type="info" icon="el-icon-circle-close" style="margin: 10px;" @click="handleClose">
      关闭
    </el-button>
    <el-button type="danger" icon="el-icon-delete" style="margin: 10px;" @click="handleDelete">
      删除
    </el-button>

    <el-form ref="inventoryEditForm" :rules="inventoryRules" :model="inventory" label-width="80px" style="margin-left:0px;">
      <el-form-item label="供应商" prop="supplierChannelId">
        <el-select v-model="inventory.supplierChannelId" clearable filterable>
          <el-option v-for="item in supplierChannelOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="货号" prop="sku">
        <el-input v-model="inventory.sku" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="尺码" prop="originalSize">
        <el-input v-model="inventory.originalSize" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="转换尺码" prop="normalizedSize">
        <el-input v-model="inventory.normalizedSize" :disabled="true" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="数量" prop="qty">
        <el-input v-model="inventory.qty" type="number" step="1" :min="0" :max="999999" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="吊牌价" prop="marketPrice">
        <el-input v-model="inventory.marketPrice" type="number" step="0.01" :min="0" :max="999999" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="成本折扣" prop="discount">
        <el-input v-model="inventory.discount" type="number" step="0.01" :min="0" :max="999999" style="width: 200px;" />
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { fetchEnum } from '@/api/system-api'
import { getInventoryById, putInventoryById, deleteInventoryById } from '@/api/manager/goods-api'
import { toOptionsList } from '@/utils/index'

export default {
  name: 'ManagerInventoryEdit',
  filters: {
  },
  data() {
    return {
      supplierChannelOptions: [],
      inventory: {
        id: null,
        supplierChannelId: null,
        sku: null,
        originalSize: null,
        normalizedSize: null,
        qty: null,
        marketPrice: null,
        discount: null
      },
      inventoryRules: {
        supplierChannelId: [{
          required: true, message: '请输入供应商', trigger: 'blur'
        }],
        sku: [{
          required: true, message: '请输入货号', trigger: 'blur'
        }],
        originalSize: [{
          required: true, message: '请输入尺码', trigger: 'blur'
        }],
        qty: [{
          required: true, message: '请输入数量', trigger: 'blur'
        }],
        marketPrice: [{
          required: true, message: '请输入吊牌价', trigger: 'blur'
        }],
        discount: [{
          required: true, message: '请输入成本折扣', trigger: 'blur'
        }]
      }
    }
  },
  async created() {
    await this.initLoad()
    await this.loadDetail()
  },
  methods: {
    async initLoad() {
      const enumCodes = ['SUPPLIER_CHANNEL']
      const { data } = await fetchEnum(enumCodes)
      for (const e of data) {
        if (e.code === 'SUPPLIER_CHANNEL') {
          if (e.options) {
            this.supplierChannelOptions = toOptionsList(e.options, 'id', 'shortName')
          }
        }
      }
    },
    loadDetail() {
      getInventoryById(this.$route.params.id).then(response => {
        const { data } = response
        this.inventory = data
      })
    },

    handleSubmit() {
      this.$refs.inventoryEditForm.validate((valid) => {
        if (!valid) {
          return
        }
        var data = JSON.parse(JSON.stringify(this.inventory))
        putInventoryById(this.inventory.id, data).then(response => {
          this.$notify({
            title: '成功',
            message: '更新成功',
            type: 'success',
            duration: 1 * 1000
          })
        }).catch(error => {
          Message({
            message: error,
            type: 'error',
            duration: 5 * 1000
          })
        })
      })
    },
    handleRefresh() {
      this.loadDetail()
    },
    handleClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    },
    handleDelete() {
      deleteInventoryById(this.inventory.id).then(response => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 1 * 1000
        })
        this.handleClose()
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
      })
    }

  }
}

</script>
